//
//  ViewController.m
//  核心动画Core Animation
//
//  Created by 伍银荣 on 2021/7/13.
//  Copyright © 2021 com.WYR. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (nonatomic, weak) CALayer *layer;
@property (nonatomic, assign) NSInteger imageName;
@end

@implementation ViewController
//轻扫手势执行的方法
- (IBAction)imageChaged:(UISwipeGestureRecognizer *)sender {
    NSLog(@"轻扫");
    
    CATransition *transition = [[CATransition alloc]init];
    //设置转场动画过渡的效果
    transition.type = @"cube";
    
    // 先确定方向,再判断值
    //从左往右
    if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
        if (self.imageName == 1) {
            NSLog(@"不能左滑了");
            return;
        } else {
            self.imageName--;
        }
        //设置转场动画的方向
        transition.subtype = kCATransitionFromLeft;
    }
    //从右往左
    else if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
        if (self.imageName == 5) {
            self.imageName = 1;
        } else {
            self.imageName++;
        }
        transition.subtype = kCATransitionFromRight;
    }
    
    self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",self.imageName]];
    
    //转场动画
    [self.imageView.layer addAnimation:transition forKey:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 20, 20)];
//    redView.backgroundColor = [UIColor redColor];
//    [self.view addSubview:redView];
//    self.layer = redView.layer;  //根layer
    
    self.imageName = 1;
    self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",self.imageName]];
    
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    

    
}

- (void)groupAnimation {
    //组动画
    CAAnimationGroup *animation = [[CAAnimationGroup alloc]init];
    //基本动画(自旋转) + 关键帧动画(绕着圆圈旋转)
    CABasicAnimation *basic = [[CABasicAnimation alloc]init];
    basic.keyPath = @"transform.rotation";
    basic.byValue = @(M_PI * 2 * 5); //在自身的基础上增加
    
    //    basic.fillMode = kCAFillModeForwards;
    //    basic.removedOnCompletion = NO;
    //    basic.repeatCount = INT_MAX;
    
    
    ///关键帧动画(绕着圆圈旋转)
    CAKeyframeAnimation *keyFrame = [[CAKeyframeAnimation alloc]init];
    keyFrame.keyPath = @"position";
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:0 endAngle:M_PI * 2 clockwise:1];
    keyFrame.path = path.CGPath;
    
    animation.animations = @[basic, keyFrame];
    //如果是组动画,设置重复次数和持续时间可以直接在组动画动设置,不需要基本动画和关键帧动画单独设置
    animation.duration = 3;
    animation.repeatCount = INT_MAX;
    
    [self.layer addAnimation:animation forKey:nil];
}

//关键帧动画
- (void)keyFrameAnimation {
    //关键帧动画
    CAKeyframeAnimation *animation = [[CAKeyframeAnimation alloc]init];
    animation.keyPath = @"position";
//    NSValue *v1 = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
//    NSValue *v2 = [NSValue valueWithCGPoint:CGPointMake(150, 100)];
//    NSValue *v3 = [NSValue valueWithCGPoint:CGPointMake(100, 150)];
//    NSValue *v4 = [NSValue valueWithCGPoint:CGPointMake(150, 150)];
    
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:0 endAngle:M_PI * 2 clockwise:1];
    animation.path = path.CGPath;
    
    //动画的持续时间
    animation.duration = 2;
    //重复次数: 无限大
    animation.repeatCount = INT_MAX;
    
    //animation.values = @[v1, v2, v3, v4];
    [self.layer addAnimation:animation forKey:nil];
}

//基本动画
- (void)basicAnimation {
    //基本动画(动画完毕后都会返回原来的位置)
    //1.创建动画对象
    CABasicAnimation *animation = [[CABasicAnimation alloc]init];
    //2.怎么做动画
    animation.keyPath = @"position.x";
    //    animation.fromValue = @10;  //从哪里
    //    animation.toValue = @(300);  //到哪里
    animation.byValue = @(10);   //在自身的基础上叠加
    
    //如果想要不返回到原来的位置,执行下面的代码
    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;
    
    //3.给谁做动画(添加动画)
    [self.layer addAnimation:animation forKey:nil];
}


@end
